package com.dexgdx.game;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputMultiplexer;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.input.GestureDetector;
import com.dexgdx.game.actor.AbstractActor;
import com.dexgdx.game.actor.Enemy;
import com.dexgdx.game.constants.RingColor;
import com.dexgdx.game.handlers.DexInputProcessor;
import com.dexgdx.game.manager.ActorManager;

public class DexGdxGame extends ApplicationAdapter {

    private OrthographicCamera camera;
	
    private ShapeRenderer renderer;
	
	@Override
	public void create () {
		renderer = new ShapeRenderer();
		camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
		camera.setToOrtho(true, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
		

		DexInputProcessor inputProcessor = new DexInputProcessor();
        InputMultiplexer im = new InputMultiplexer();
        GestureDetector gd = new GestureDetector(inputProcessor);
        im.addProcessor(gd);
        im.addProcessor(inputProcessor);
        
        
        Gdx.input.setInputProcessor(im);
        
		Gdx.graphics.setContinuousRendering(false);
		Gdx.graphics.requestRendering();
		
		Game.initialize();
		Game.run();
	}

	@Override
	public void render () {
		
		Gdx.gl.glClearColor(0, 0, 0, 1);
		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
		Gdx.gl.glLineWidth(6);
		camera.update();
		renderer.setProjectionMatrix(camera.combined);
		
//		if(System.currentTimeMillis() - Game.renderTimer > 400){
//			
//			Game.renderTimer = System.currentTimeMillis();
			
//			ActionManager.runNextInQueue();
			
			if(Game.selectionRectangle.isVisible()){
				renderer.begin(ShapeType.Line);
				renderer.setColor(Color.valueOf(RingColor.SELECTED_PLAYER.getHexCode()));
				renderer.box(Game.selectionRectangle.x, Game.selectionRectangle.y, 0, Game.selectionRectangle.width, Game.selectionRectangle.height, 0);
				renderer.end();
			}
			
			for (AbstractActor actor : Game.actionData.getTargetOwners()) {
				if(!(actor.getMovingTarget() instanceof Enemy)){
					renderer.begin(ShapeType.Line);
					renderer.setColor(Color.valueOf(RingColor.BEING_TARGET.getHexCode()));
					renderer.ellipse(actor.getMovingTarget().getX(), actor.getMovingTarget().getY(), actor.getMovingTarget().getWidth(), actor.getMovingTarget().getHeight());
					renderer.end();
				}
			}

			for (Long actorIndex : Game.actorData.getEnemyList()) {
				AbstractActor actor = ActorManager.getActor(actorIndex);
				 
				renderer.begin(ShapeType.Line);
				renderer.setColor(Color.valueOf(actor.getRingColor().getHexCode()));
				if(actor.getActorStatus().isBeingMovedTo() || actor.getActorStatus().isBeingAttackedTo())
					renderer.ellipse(actor.getX() + 3, actor.getY() + 3, actor.getWidth() - 6, actor.getHeight() - 6);
				else
					renderer.ellipse(actor.getX(), actor.getY(), actor.getWidth(), actor.getHeight());
				renderer.end();
			}

			for (Long actorIndex : Game.actorData.getPlayerList()) {
				AbstractActor actor = ActorManager.getActor(actorIndex);
				 
				renderer.begin(ShapeType.Line);
				renderer.setColor(Color.valueOf(actor.getRingColor().getHexCode()));
				renderer.ellipse(actor.getX(), actor.getY(), actor.getWidth(), actor.getHeight());
				renderer.end();
			}
//			batch.begin();
//			batch.draw(img, 0, 0);
//			batch.end();
//		}
	}
}
